/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.figures;

import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.RGB;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.helper.ModelerHelper;

/**
 * this is figure to represent the shape of a streotyped element.
 * It is constituted of a icon and text for the name of the label.
 * 
 * @author Patrick Tessier
 * @since 14 sept. 06
 */
public class IconFigure extends Label {
	
	/**
	 * 
	 */
	protected String name="";
	
	/**
	 * 
	 */
	protected Image originalimage;
	
	/**
	 * 
	 */
	protected Image adaptedImage;
	
	/**
	 * 
	 */
	protected int numerNode; 
	
	/**
	 * 
	 */
	protected String stereotypes="";
	
	/**
	 * create an icon figure.
	 * 
	 * @param numberNode is used to spcify a unique identfier tha t is associated to the image thanks
	 * to the graphNode.hashCode()
	 * @param image the image (GIF) that we want to see
	 */
	public IconFigure(Image image, int numberNode)
	{ 
		super("");
		this.originalimage=image;
		this.numerNode=numberNode;
		FontData[] fontdata={new FontData("Arial", 9, SWT.NORMAL)};
		Font font = PapyrusPlugin.fontManager.get(fontdata);
		setFont(PapyrusPlugin.fontManager.get(fontdata));	
		this.setBackgroundColor(PapyrusPlugin.colorManager.get(new RGB(215,212,244)));
		this.setIconAlignment(PositionConstants.CENTER);
		this.setTextAlignment(PositionConstants.CENTER);
		this.setIcon(image);
		this.setIconTextGap(2);
	}
	
/*
 * (non-Javadoc)
 * @see org.eclipse.draw2d.Figure#setBounds(org.eclipse.draw2d.geometry.Rectangle)
 */
	/**
 * 
 * 
 * @param rect 
 */
public void setBounds(Rectangle rect) {
		super.setBounds(rect);
		
		// construct the key
		String key= "icon"+numerNode;
		
		//look for the image
		ImageData imdata=null;
		if( stereotypes==""){
		imdata=(ImageData)originalimage.getImageData().scaledTo(rect.width, rect.height-15).clone();
		}
		else{
			imdata=(ImageData)originalimage.getImageData().scaledTo(rect.width, rect.height-30).clone();
		}
		Image imm=PapyrusPlugin.getDefault().getImageRegistry().get(key);
		
		//no image in the registry
		if (imm==null){
			PapyrusPlugin.getDefault().getImageRegistry().put(key,ImageDescriptor.createFromImageData(imdata));
			imm=PapyrusPlugin.getDefault().getImageRegistry().get(key);
		}
		//there is an image but we would like replace it by our new image
		else{
			//imm.dispose();
			PapyrusPlugin.getDefault().getImageRegistry().remove(key);
			PapyrusPlugin.getDefault().getImageRegistry().put(key,ImageDescriptor.createFromImageData(imdata));
			imm=PapyrusPlugin.getDefault().getImageRegistry().get(key);
		}
		// in the case where the image was not disposed
		if(adaptedImage!=null){
			adaptedImage.dispose();
			adaptedImage=null;
		}
		//set new image
		adaptedImage= imm;
		this.setIcon(adaptedImage);
	}
	
	
	/**
	 * 
	 */
	public void refreshstrings(){
		this.setText(this.stereotypes+name);	
	}
	
	/**
	 * set the name of the figure.
	 * 
	 * @param name the name of the figure
	 */
	public void setName(String name){
		
		this.name=name;
		this.setTextPlacement(PositionConstants.SOUTH);
		this.setIconAlignment(PositionConstants.CENTER);
		refreshstrings();
	}
	
	/**
	 * Refresh stereotypes.
	 * 
	 * @param presentation the presentation
	 * @param hasIcon the has icon
	 * @param hasShape the has shape
	 * @param stereotypes the stereotypes
	 */
	public void refreshStereotypes(String stereotypes, String presentation, boolean hasIcon, boolean hasShape) {
		
		if (stereotypes.equals("")) {
			this.setStereotypes(null);
			return;
		}

		if (presentation.equals(ModelerHelper.ICON_STEREOTYPE_PRESENTATION) && hasIcon) {
			this.setStereotypes(null);
			return;
		} else if (presentation.equals(ModelerHelper.IMAGE_STEREOTYPE_PRESENTATION) && hasShape) {
			this.setStereotypes(null);
			return;
		}

		this.setStereotypes(stereotypes);//add stereotypes on the class figure		
		return;
	}

	/**
	 * 
	 * 
	 * @param stereotypes 
	 */
	public void setStereotypes(String stereotypes){
	if (stereotypes==null||stereotypes==""){
		this.stereotypes="";
	}
	//TODO: modify to center name 
	else{this.stereotypes=PapyrusPlugin.ST_LEFT+stereotypes+PapyrusPlugin.ST_RIGHT+"\n    ";}
	refreshstrings();
	}
}